package com.wang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.mall.bean.Product;
import com.wang.mall.bean.ProductImage;
import com.wang.mall.mapper.ProductMapper;
import com.wang.mall.service.ProductImageService;
import com.wang.mall.service.ProductService;
import com.wang.mall.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    @Autowired
    private ProductImageService productImageService;

    @Override
    public PageUtils queryPage(Integer page, Integer limit) {
        IPage<Product> page1 = page(new Page<Product>(page, limit), new LambdaQueryWrapper<Product>());

        List<Product> records = page1.getRecords();
        List<Product> collect = records.stream().map(product -> {
            List<ProductImage> list = productImageService.list(new LambdaQueryWrapper<ProductImage>().eq(ProductImage::getProductId, product.getId()));
            product.setImageList(list);
            return product;
        }).collect(Collectors.toList());


        page1.setRecords(collect);

        return new PageUtils(page1);
    }

    @Override
    public Product findById(Long skuId) {
        Product product = getById(skuId);
        product.setImageList(productImageService.list(new LambdaQueryWrapper<ProductImage>().eq(ProductImage::getProductId, skuId)));
        return product;
    }
}
